package com.ktg.system.service.impl;

import java.util.Arrays;
import java.util.List;

import com.ktg.common.core.domain.Result;
import com.ktg.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.system.utils.SecurityUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktg.system.mapper.SysDataSourceMapper;
import com.ktg.system.domain.SysDataSource;
import com.ktg.system.service.ISysDataSourceService;



/**
 * 数据源Service业务层处理
 * 
 * @author byteco
 * @date 2022-08-26
 */
@Service
public class SysDataSourceServiceImpl extends ServiceImpl<SysDataSourceMapper, SysDataSource> implements ISysDataSourceService
{
    @Autowired
    private SysDataSourceMapper sysDataSourceMapper;


    /**
     * 查询数据源
     * 
     * @param entity 数据源
     * @param page 分页对象
     * @return 数据源
     */
    @Override
    public IPage<SysDataSource> queryPageList(SysDataSource entity, Page<SysDataSource> page) {
        LambdaQueryWrapper<SysDataSource> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(SysDataSource::getCreateTime);
        IPage<SysDataSource> pageList = this.page(page, lqw);
        return pageList;
    }

    /**
     * 查询数据源
     *
     * @param entity 数据源
     * @return 数据源
     */
    @Override
    public List<SysDataSource> queryList(SysDataSource entity) {
        LambdaQueryWrapper<SysDataSource> lqw = buildQueryWrapper(entity);
        lqw.orderByDesc(SysDataSource::getCreateTime);
        return this.list(lqw);
    }

    public SysDataSource getInfo(String id)
    {
        QueryWrapper<SysDataSource> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysDataSource::getId, id);
        SysDataSource entity = this.getOne(queryWrapper);
        if (StringUtils.isNotBlank(entity.getDbPassword())) {
            String encrypt = SecurityUtil.jiemi(entity.getDbPassword());
            entity.setDbPassword(encrypt);
        }
        return entity;
    }


    /**
     * 新增数据源
     * 
     * @param entity 数据源
     * @return 结果
     */
    @Override
    public Result insert(SysDataSource entity)
    {
        try {
            long count = checkDbCode(entity.getCode());
            if (count > 0) {
                return Result.ERROR("数据源编码已存在");
            }
            String dbPassword = entity.getDbPassword();
            if (StringUtils.isNotBlank(dbPassword)) {
                String encrypt = SecurityUtil.jiami(dbPassword);
                entity.setDbPassword(encrypt);
            }
            entity.setCreateTime(DateUtils.getNowDate());
            save(entity);

        } catch (Exception e) {
            log.error("系统一次：",e);
        }
        return Result.OK("添加成功！");
    }

    /**
     * 修改数据源
     * 
     * @param entity 数据源
     * @return 结果
     */
    @Override
    public Result update(SysDataSource entity)
    {
        try {
            SysDataSource d = getById(entity.getId());
            String dbPassword = entity.getDbPassword();
            if (StringUtils.isNotBlank(dbPassword)) {
                String encrypt = SecurityUtil.jiami(dbPassword);
                entity.setDbPassword(encrypt);
            }
            entity.setUpdateTime(DateUtils.getNowDate());
            updateById(entity);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.OK("编辑成功!");

    }

    /**
     * 批量删除数据源
     * 
     * @param ids 需要删除的数据源主键
     * @return 结果
     */
    @Override
    public boolean delete(String[] ids)
    {
        return this.removeByIds(Arrays.asList(ids));
    }

    /**
     * 删除数据源信息
     * 
     * @param id 数据源主键
     * @return 结果
     */
    @Override
    public boolean delete(String id)
    {
        return this.removeById(id);
    }

    private LambdaQueryWrapper<SysDataSource> buildQueryWrapper(SysDataSource entity) {
        LambdaQueryWrapper<SysDataSource> lqw = Wrappers.lambdaQuery();
                    lqw.eq(StringUtils.isNotBlank(entity.getCode()), SysDataSource::getCode, entity.getCode());
                    lqw.like(StringUtils.isNotBlank(entity.getName()), SysDataSource::getName, entity.getName());
                    lqw.like(StringUtils.isNotBlank(entity.getDbName()), SysDataSource::getDbName, entity.getDbName());
        return lqw;
    }

    /**
     * 检查数据源编码是否存在
     *
     * @param dbCode
     * @return
     */
    private long checkDbCode(String dbCode) {
        QueryWrapper<SysDataSource> qw = new QueryWrapper();
        qw.lambda().eq(true, SysDataSource::getCode, dbCode);
        return count(qw);
    }
}
